from rest_framework import serializers
from .models import Group
from .models import SysUser  # 导入 SysUser 模型
from .models import Task, Subtask, ForumPost    # 导入 Task 模型


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = SysUser
        fields = (
            'user_id', 'username',  'email', 'avatar', 'status', 'role',
            'ability', 'enthusiasm', 'communication', 'popularity', 'credit', 'times'
        )

class GroupSerializer(serializers.ModelSerializer):
    # 使用 SysUser 的序列化器来表示组长
    leader = serializers.StringRelatedField()  # 仅显示组长的用户名，或根据需求返回更多字段
    members = serializers.StringRelatedField(many=True)  # 获取组员用户名列表
    group_task = serializers.StringRelatedField()  # 显示任务名称，或通过任务的详细信息进行序列化

    class Meta:
        model = Group
        fields = ('id', 'group_name', 'leader', 'group_task', 'members')


class TaskSerializer(serializers.ModelSerializer):
    # 序列化关联的教师和负责任务的学生（leaders）
    teacher = serializers.StringRelatedField()  # 仅显示教师的用户名，或根据需求返回更多字段
    leaders = serializers.StringRelatedField(many=True)  # 获取负责该任务的学生用户名列表

    class Meta:
        model = Task
        fields = (
            'id', 'title', 'description', 'min_group_size', 'max_group_size', 'deadline',
            'scoring_criteria', 'sample', 'status', 'teacher', 'created_at', 'updated_at',
            'leaders', 'students_number'
        )

class SubtaskSerializer(serializers.ModelSerializer):
    group_member = serializers.StringRelatedField()  # 显示小组成员的字符串表示
    task = serializers.StringRelatedField()  # 显示任务的标题或更多字段
    created_by = serializers.StringRelatedField()  # 显示创建者的用户名
    assigned_to = serializers.StringRelatedField(source='assigned_to.username')  # 显示分配给的用户名

    class Meta:
        model = Subtask
        fields = ['id', 'group_member', 'task', 'created_by', 'assigned_to', 'start_date', 'end_date', 'description', 'created_at', 'updated_at', 'status']


class ForumPostSerializer(serializers.ModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = ForumPost
        fields = [
            'id',
            'title',
            'content',
            'category',
            'author',
            'attachment',  # 附件可以返回文件 URL
            #'summary',
            'created_at'
        ]